/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.activity;

import java.util.Set;

import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.resource.DataSource;
import com.cleangwt.showcase.client.resource.i18n.ShoppingManagerMessages;
import com.cleangwt.user.cellview.client.CellGrid;
import com.cleangwt.user.client.activity.ForwardableActivity;
import com.cleangwt.user.client.ui.Lay;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.MultiSelectionModel;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@SuppressWarnings("deprecation")
public class CwShoppingCart extends ForwardableActivity implements ProvidesSource {

  private class ProductCell extends AbstractCell<SimpleObject> {

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context, SimpleObject value,
        SafeHtmlBuilder sb) {
      if (selectionModel.isSelected(value)) {
        sb.append(template.selected((String) value.get("name"), value.getInt("price"),
            (String) value.get("desc")));
      } else {
        sb.append(template.unselected((String) value.get("name"), value.getInt("price"),
            (String) value.get("desc")));
      }
    }
  }

  interface Template extends SafeHtmlTemplates {
    @SafeHtmlTemplates.Template("<div> " + "<a class='thumbnail' style='height:86px;'></a>"
        + "<div class='caption'><h4>{0}</h4></div>"
        + "<div class=''>${1}</div><div class=''>{2}</div>"
        + "<input tab-index='-1' type='checkbox' checked/>" + "</div>")
    SafeHtml selected(String name, int price, String desc);

    @SafeHtmlTemplates.Template("<div> " + "<a class='thumbnail' style='height:86px;'></a>"
        + "<div class='caption'><h4>{0}</h4></div>"
        + "<div class=''>${1}</div><div class=''>{2}</div>"
        + "<input tab-index='-1' type='checkbox'/>" + "</div>")
    SafeHtml unselected(String name, int price, String desc);
  }

  private static Template template = GWT.create(Template.class);

  private ShoppingManagerMessages c = GWT.create(ShoppingManagerMessages.class);

  private Button btnPayUp = new Button(c.payup());
  private MultiSelectionModel<SimpleObject> selectionModel =
      new MultiSelectionModel<SimpleObject>();

  public CwShoppingCart() {
    CellGrid cellList = new CellGrid(new ProductCell());

    initWidget(Lay.flow(cellList, Lay.pullRight(btnPayUp)));

    cellList.setSelectionModel(selectionModel, DefaultSelectionEventManager
        .<SimpleObject> createCheckboxManager());
    cellList.setRowData(0, DataSource.getProducts());

    btnPayUp.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        CwShoppingPayup payup = getActivity(1);
        payup.setProducts(getSelectedProcudts());
        forward(1);
      }
    });
  }

  public Set<SimpleObject> getSelectedProcudts() {
    return selectionModel.getSelectedSet();
  }
}
